|
It's unlikely that you will be coding cars, animals or bank accounts in the real world, so in this tutorial we will look at real life classes, how they inherit from a base class, how they override methods and most importantly WHY. For this tutorial we are going to create a set of classes that will scan a folder for files and then send them via a specified protocol across the internet. We will be using object orientation, inheritance, xml configuration files, class libraries, data abstraction, exception handling, events and a few more techniques. Don't Panic! I will walk you through each of these concepts slowly and explain what is going on! I will also talk you through the thought processes behind the method I am using so this should give you an understanding of software design as well. This tutorial does not teach you a concept such as events, but rather how to use the concept. You should refer to the C# Tutorials page on the concept to learn about it. The full source code and XML configuration files can be downloaded at the bottom of this page. Requirements SpecificationOur application is going to need to scan a folder on the hard drive and for each file send it accross the internet using a specified protocol (email or http post). The application should be written so that an inexperienced coder can use the code to send files and configuration should be stored within xml files. The ApproachThis application calls for two projects – a class library where all the work is done, and a console application that will use the class library. The approach will allow the bulk of the code (from the class library) to be reused in other projects – for example a web service or windows application. Using class libraries we can create a satellite assembly (.dll) that is independent of the program referencing (using) it. We can also have multiple applications referencing the same assembly if we wanted to. All of the settings will be stored within an xml configuration file so that it can be configured by a non-programmer without having to be re-compiled. Think about the class structureIts worth taking some time to think about how the classes are going to be structured. I say classes because there is going to be more than one. I can already visualise that we are going to have a class to send via email, and a second class to send via http post. The next thing to think about is the data that these classes will require and the actions that will be performed. This is best done by writing down on paper the class name (email and http post) and underneath listing all the data items and actions. I have tabularised the main items below, there may well be (and often are) more items as the project progresses.
You should notice that there are a few items that are common to both classes. Rather than code two classes with the same functionality, it would be better to take advantage of inheritance by creating a base class that our two other classes can inherit common properties from the base class.
We can now see that the email class will be a completely separate and independent from the http class, but since they both inherit from the base class they will share some commonality. In the next tutorial we will create a Visual Studio project and see how to transform this data into a set of classes as well as how to set up inheritance.
Download the source code:FileSendExample.zip
Tutorials in this Series
Leave a ReplyClick here to cancel reply.
|







